package edu.zut.cs.network.exercise.practice;

import java.util.Arrays;
import java.util.Scanner;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import edu.zut.cs.network.exercise.base.BaseExample;

public class Student extends BaseExample {

//	圆柱体积计算
	public void cylinder_volume() {

		this.logger.debug("请输入圆柱体的底面半径r");
		Scanner s = new Scanner(System.in);
		double r = s.nextDouble();
		this.logger.debug("请输入圆柱体的高h");
		double h = s.nextDouble();
		this.logger.debug("圆柱体的体积V=" + Math.PI * Math.pow(r, 2) * h);
	}

//	整数转换ASCII字符 
	public void intToASCII() {
		this.logger.debug("请输入一个0-127之间的整数：");
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		this.logger.debug("该整数的ASCII字符为为：" + (char) n);
	}

//	大写字母转化成小写字母
	public void wordChange() {
		// 如果输入的是大写，+32即可得到小写
		this.logger.debug("请输入一个要转换的大写字母：");
		Scanner s = new Scanner(System.in);
		char n = s.next().charAt(0);
		if (n >= 'A' && n <= 'Z') {
			n += 32;
			this.logger.debug("您输入的大写字母" + (char) (n - 32) + "被转换成了" + "'" + n + "'");
		} else {
			this.logger.debug("输入的字符有误！！");
		}
	}

//	整数位数和
	public void sum() {
		this.logger.debug("请输入一个整数(0-999):");
		Scanner s = new Scanner(System.in);
		String n = s.next();

		int intValue = Integer.parseInt(n);
		int i1 = intValue / 100;
		int i2 = intValue / 10 - i1 * 10;
		int i3 = intValue - i2 * 10 - i1 * 100;
		int sum = i1 + i2 + i3;
		this.logger.debug("该整数的各位数之和为：" + sum);
	}

//	整数排序
	public void sort() {
		int[] arr = new int[3]; // 存储输入数据
		int temp; // 用于数据交换
		this.logger.debug("请输入三个整数:");
		Scanner scan = new Scanner(System.in);// 接收输入数据
		for (int i = 0; i < arr.length; i++) { // 数据写入数组
			arr[i] = scan.nextInt();
		}

		Arrays.sort(arr);
		this.logger.debug("排序后的结果为：" + arr[0] + "<" + arr[1] + "<" + arr[2]);

		/*
		 * for (int i = 0; i < arr.length; i++) { // 数据比较大小交换 for (int j = 0; j < i;
		 * j++) { if (arr[i] < arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
		 * } } // sum += arr[i]; } for (int k = 0; k < arr.length; k++) {
		 * this.logger.debug(arr[k]); }
		 */
	}

//	整除判断
	public void divide_judge() {

		this.logger.debug("请输入一个整数:");
		Scanner scan = new Scanner(System.in);
		int i = scan.nextInt();
		if (i % 5 == 0 && i % 6 == 0)
			this.logger.debug(i + " is divisible by both 5 and 6");
		else if (i % 5 != 0 && i % 6 != 0)
			this.logger.debug(i + " is not divisible by either 5 or 6");
		else
			this.logger.debug(i + " is divisible by 5 or 6, but not both");
	}

//	输出能被5、6同时整除的整数
	public void divisible_integer() {

		this.logger.debug("能被5和6同时整除的所有整数(100~1000):");
		int count = 0;
		for (int i = 100; i < 1000; i++) {
			if (i % 5 == 0 && i % 6 == 0) {
				this.logger.debug(i);
				if (count % 10 == 9) {
					this.logger.debug("\n");
				}
				count++;
			}
		}
	}

//	嵌套循环
	public void nesting_loop() {
		for (int i = 1; i <= 6; i++) {
			for (int j = 1; j <= 7 - i; j++)
				this.logger.debug(j + " ");
			this.logger.debug("\n");
		}
	}

//	表达式求和
	public void countForSum() {
		double sum = 0, a = 1, b;
		for (a = 1; a <= 97; a++) {
			b = a + 2;
			sum = sum + a / b;
			a++;
		}
		this.logger.debug("计算1/3 + 3/5 + 5/7 + 7/9 + … +97/99的结果为：" + sum);
	}

//	计算一个整数中的各个数字的和
	public static int sumDigits(long n) {
		int result = 0;
		while (n != 0) {
			result += n % 10;
			n /= 10;
		}
		return result;
	}

	public void sum_degits() {
		this.logger.debug("请输入一个数字：");
		Scanner input = new Scanner(System.in);
		long n = input.nextLong();
		this.logger.debug("您输入的数字是：" + n + "，各位数字之和为" + sumDigits(n) + "\n");

	}

//	英尺与米的转换		meter=0,305 * foot
	public static double footToMeter(double foot) {
		double n = foot * 0.305;
		n = Math.round(n * 1000) / 1000.0;
		return n;
	}

	public static double meterToFoot(double meter) {
		double n = meter / 0.305;
		n = Math.round(n * 1000) / 1000.0;
		return n;
	}

	public void convert() {
		double a = 1.0;
		this.logger.debug("Feet" + "\t" + "Meters");
		for (int i = 0; i < 9; i++) {
			this.logger.debug(a + "\t" + footToMeter(a));
			a++;
		}
		this.logger.debug("\n");
		double b = 20.0;
		this.logger.debug("Meters" + "\t" + "Feet");
		for (int i = 0; i < 9; i++) {
			this.logger.debug(b + "\t" + meterToFoot(b));
			b = b + 5.0;
		}
	}

//	指定输出ch1和ch2之间的所有字符
	public static void printChars(char ch1, char ch2, int numberPerLine) {
		Logger logger = LogManager.getLogger(Student.class);
		int n = 0;
		for (; ch1 <= ch2; ch1++) {
			logger.debug(ch1);
			n++;
			if (n == numberPerLine) {
				n = 0;
				logger.debug("\n");
			}
		}
	}

	public void printForchars() {
		this.logger.debug("以每行numberPerLine个字符的方式输出ch1和ch2之间的所有字符");
		String ch1, ch2;
		Scanner s = new Scanner(System.in);
		this.logger.debug("请输入ch1:");
		ch1 = s.next();
		char c1 = ch1.charAt(0);
		this.logger.debug("请输入ַch2:");
		ch2 = s.next();
		char c2 = ch2.charAt(0);
		this.logger.debug("请输入numberPerLine:");
		int num;
		num = s.nextInt();
		printChars(c1, c2, num);
	}

//	全1矩阵
	public static void printMatrix(int n) {
		Logger logger = LogManager.getLogger(Student.class);
		for (int i = 0; i < n; i++) {
			for (int j = 1; j <= n; j++) {
				logger.debug("1 ");
			}
			logger.debug("\n");
		}
	}

	public void printForMatrix() {
		this.logger.debug("请输入矩阵行数/列数：");
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		printMatrix(n);
	}

//	整数查重
	public void rejectPrint() {
		int[] a = new int[100];
		int b;
		this.logger.debug("请输入十个整数：֣");
		for (int i = 0; i < 10; ++i) {
			Scanner s = new Scanner(System.in);
			a[i] = s.nextInt();
		}
		this.logger.debug(a[0] + " ");
		for (int i = 1; i < 10; ++i) {
			boolean flag = true;
			for (int m = 0; m < i; ++m) {
				if (a[i] == a[m]) {
					flag = false;
					break;
				}
			}
			if (flag)
				this.logger.debug(a[i] + " ");
		}
	}

//	统计随机数出现次数
	public void countForRandom() {
		this.logger.debug("统计随机产生的100个在0~9的随机整数每个整数出现的次数");
		int[] array = new int[100];
		int[] count = new int[10];
		for (int i = 0; i < 100; i++) {
			array[i] = (int) (Math.random() * 10);
		}
		for (int i = 0; i < 10; i++) {
			count[i] = 0;
			for (int j = 0; j < 100; j++) {
				if (array[j] == i) {
					count[i]++;
				}
			}
			this.logger.debug("Count for " + i + " is " + count[i]);
		}
	}

//	返回一个整数数组中最小值的位置
	public static int minIndex(int[] list) {
		int location = 0;
		int min = list[0];
		for (int i = 1; i < list.length; ++i) {
			if (list[i] < min) {
				min = list[i];
				location = i;
			}
		}
		return location + 1;
	}

	public void minLocation() {
		this.logger.debug("请输入数字个数n:");
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		this.logger.debug("请输入各位数字：");
		for (int i = 0; i < a.length; i++)
			a[i] = in.nextInt();
		this.logger.debug("最小的数字位置为：" + minIndex(a));
	}

//	计算二维数组对角线元素和
	public static int Dsum(int list[][]) {
		int sum = 0;
		for (int i = 0; i < list.length; i++) {
			for (int j = 0; j < list[i].length; j++)
				if (i == j)
					sum += list[i][j];
		}
		return sum;
	}

	public void diagonalSum() {
		// TODO Auto-generated method stub

		this.logger.debug("请输入矩阵的行数与列数(行列保持一致):");
		int m, n;
		Scanner s = new Scanner(System.in);
		m = s.nextInt();
		n = s.nextInt();
		this.logger.debug("请输入矩阵：");
		int[][] a = new int[m][n];
		for (int i = 0; i < a.length; i++)
			for (int j = 0; j < a[i].length; j++)
				a[i][j] = s.nextInt();
		this.logger.debug("对角线和为：" + Dsum(a));
	}

//	2个二维数组的加法运算
	public static int[][] matrix(int[][] m, int[][] n) {
		int[][] a = new int[m.length][m[1].length];
		for (int i = 0; i < m.length; ++i) {
			for (int j = 0; j < m[1].length; ++j) {
				a[i][j] = m[i][j] + n[i][j];
			}
		}
		return a;
	}

	public void matrixSum() {
		int[][] m = { { 1, 8, 5, 9, 3 }, { 4, 0, 0, 9, 8 }, { 9, 1, 3, 7, 3 }, { 1, 3, 4, 6, 4 }, { 9, 5, 1, 1, 2 } };
		int[][] n = { { 7, 6, 7, 9, 0 }, { 6, 3, 8, 8, 1 }, { 0, 0, 6, 1, 6 }, { 7, 4, 3, 8, 6 }, { 2, 4, 6, 3, 1 } };
		int[][] a = new int[5][5];
		a = matrix(m, n);
		this.logger.debug("The addition of the matrices is");
		for (int i = 0; i < m.length; ++i) {
			for (int j = 0; j < m[1].length; ++j) {
				this.logger.debug(" " + m[i][j]);
			}
			if (i == 2) {
				this.logger.debug("  +  ");
			} else {
				this.logger.debug("     ");
			} // m1
			for (int j = 0; j < m[1].length; ++j) {
				this.logger.debug(" " + m[i][j]);
			}
			if (i == 2) {
				this.logger.debug("  =  ");
			} else {
				this.logger.debug("     ");
			} // m2
			for (int j = 0; j < m[1].length; ++j) {
				this.logger.debug(" " + a[i][j]);
			}
			this.logger.debug("\n");// m3
		}
	}

//	计算一个字符串中字母的个数
	public int countLetters(String s) {
		int number = 0;
		char[] a = s.toCharArray();
		for (int i = 0; i < a.length; ++i) {
			if ((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')) {
				++number;
			}
		}
		return number;
	}

	public void countChars() {
		// TODO Auto-generated method stub
		this.logger.debug("请输入字符串内容：");
		Scanner scan = new Scanner(System.in);
		String s = scan.nextLine();
		int number = countLetters(s);
		this.logger.debug("该字符串中字母的个数为：" + number);
	}

//	二进制数到十进制数的转换
	public static int parseBinary(String binaryString) {
		int x = 0;
		for (char a : binaryString.toCharArray()) {
			x = x * 2 + (a == '1' ? 1 : 0);
		}
		return x;
	}

	public void parse_binary() {
		this.logger.debug("请输入要转化的二进制:");
		Scanner s = new Scanner(System.in);
		String binaryString = s.next();
		this.logger.debug("二进制转化为十进制:" + parseBinary(binaryString));
	}

//	十进制数到二进制数的转换
	public static String convertDecimalToBinary(int value) {
		String s = Integer.toBinaryString(value);
		return s;
	}

	public void toBinary() {
		Scanner scanner = new Scanner(System.in);
		this.logger.debug("请输入你要转化的十进制:");
		int value = scanner.nextInt();
		this.logger.debug("十进制转化为二进制：" + convertDecimalToBinary(value));
	}

//	计算整数和
	public static int main(int[] allInt) {
		int sum = 0;
		int len = allInt.length;
		for (int i = 0; i < len; i++) {
			sum += allInt[i];
		}
		return sum;
	}

	public void calInteger() {
		int[] s = { 1, 2, 3, 4, 5, 6, 7 };
		this.logger.debug("整数和为： " +main(s));
	}

	
}
